package com.party.admin.web.controller.sign;

import com.google.common.base.Strings;
import com.party.admin.biz.sign.SignGroupBizService;
import com.party.admin.utils.RealmUtils;
import com.party.admin.web.dto.AjaxResult;
import com.party.admin.web.dto.input.sign.SignGroupInput;
import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.sign.GroupAuthor;
import com.party.core.model.sign.SignApply;
import com.party.core.model.sign.SignGroup;
import com.party.core.model.sign.SignProject;
import com.party.core.service.sign.ISignApplyService;
import com.party.core.service.sign.ISignGroupService;
import com.party.core.service.sign.ISignProjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 签到小组控制器
 * Created by wei.li
 *
 * @date 2017/6/6 0006
 * @time 10:45
 */

@Controller
@RequestMapping(value = "sign/group")
public class SignGroupController {

    @Autowired
    private ISignGroupService signGroupService;

    @Autowired
    private SignGroupBizService signGroupBizService;

    @Autowired
    private ISignProjectService signProjectService;

    @Autowired
    private ISignApplyService signApplyService;


    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 签到小组列表
     * @param signGroupInput 签到小组
     * @param page 分页参数
     * @return 交互数据
     */
    @RequestMapping(value = "list")
    public ModelAndView list(SignGroupInput signGroupInput, Page page){
        page.setLimit(20);
        ModelAndView modelAndView = new ModelAndView("sign/signGroupList");
        List<GroupAuthor> list = signGroupBizService.list(signGroupInput, page);
        SignProject signProject = signProjectService.get(signGroupInput.getProjectId());
        modelAndView.addObject("signProject", signProject);
        modelAndView.addObject("list", list);
        modelAndView.addObject("page", page);
        return modelAndView;
    }


    /**
     * 签到小组查询
     * @param id 编号
     * @return 交互数据
     */
    @RequestMapping(value = "view")
    public ModelAndView view(String id, String projectId){
        ModelAndView modelAndView = new ModelAndView("sign/signGroupView");
        SignGroup signGroup;
        if (Strings.isNullOrEmpty(id)){
            signGroup = new SignGroup();
            signGroup.setProjectId(projectId);
        }
        else {
            signGroup = signGroupService.get(id);
        }
        modelAndView.addObject("signGroup", signGroup);
        SignProject signProject = signProjectService.get(projectId);
        modelAndView.addObject("signProject", signProject);
        return modelAndView;
    }


    /**
     * 保存签到小组
     * @param signGroup 签到小组
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "save")
    public AjaxResult save(SignGroup signGroup) {
        try {
            AjaxResult ajaxResult = new AjaxResult(true);
            MemberGroup member = RealmUtils.getCurrentUser();
            if (Strings.isNullOrEmpty(signGroup.getId())) {
                signGroup.setCreateBy(member.getId());
                signGroup.setUpdateBy(member.getId());
                signGroupService.insert(signGroup);
            } else {
                signGroup.setUpdateBy(member.getId());
                signGroupService.update(signGroup);
            }
            return ajaxResult;
        } catch (Exception e) {
            logger.error("保存小组异常", e);
            return AjaxResult.error("保存小组异常");
        }
    }

    /**
     * 删除签到小组
     * @param id 编号
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "delete")
    public AjaxResult delete(String id){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            List<SignApply> signApplies = signApplyService.findByGroup(id);
            if (signApplies.size() > 0) {
                return AjaxResult.error("小组已有成员，不能删除");
            }
            signGroupService.delete(id);
        } catch (Exception be) {
            ajaxResult.setSuccess(false);
            ajaxResult.setDescription(be.getMessage());
            return ajaxResult;
        }
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    /**
     * 小组排行
     *
     * @param groupAuthor 项目id
     * @param type      日期类型
     * @param page      分页参数
     * @return
     */
    @RequestMapping("rankList")
    public ModelAndView rankList(GroupAuthor groupAuthor, Integer type, Page page) {
        page.setLimit(20);
        ModelAndView mv = new ModelAndView("sign/signGroupRank");
        List<GroupAuthor> groupAuthorList = signGroupBizService.rankListXcx(groupAuthor, type, page);
        mv.addObject("groupList", groupAuthorList);
        SignProject signProject = signProjectService.get(groupAuthor.getProjectId());
        mv.addObject("signProject", signProject);
        mv.addObject("type", type);
        return mv;
    }

    /**
     * 移除队伍
     *
     * @param applyId 报名id
     * @return
     */
    @ResponseBody
    @RequestMapping("cancelJoinGroup")
    public AjaxResult cancelJoinGroup(String applyId) {
        if (StringUtils.isEmpty(applyId)) {
            return AjaxResult.error("报名id不能为空");
        }
        try {
            signGroupBizService.cancelJoinGroup(applyId);
            return AjaxResult.success();
        } catch (Exception e) {
            logger.error("取消分组异常", e);
            return AjaxResult.error("取消分组异常");
        }
    }
}
